"""
Author        yongfa
Date          2022-07-22 14:30:41
LastEditTime  2023-07-08 11:18:06
LastEditors   yongfa
Description   数据字典
"""

from flask_restx import Resource

from app.extensions.decorators import casbin_required
from .models import Dict, DictType
from .parameter import dicts_params
from .services import DictTypeService, DictService


def configure_router(ns):

    @ns.route("/types")
    class DictTypeListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=dicts_params['dict_type_list_params'], description="获取数据字典类型列表")
        @ns.expect(dicts_params['dict_type_list_params'])
        def get(self):
            """获取数据字典类型列表"""
            return DictTypeService(model=DictType, args=dicts_params['dict_type_list_params'], only_parent=False).get_list()

        @ns.doc(parser=dicts_params['dict_type_params'], description="添加数据字典类型")
        @ns.expect(dicts_params['dict_type_params'])
        def post(self):
            """添加数据字典类型"""
            return DictTypeService(model=DictType, args=dicts_params['dict_type_params']).create()

    @ns.route("/types/<int:id>")
    @ns.doc(params={"id": "数据字典类型id"})
    class DictTypeResource(Resource):
        method_decorators = [casbin_required]

        def get(self, id):
            """获取数据字典类型详情"""
            return DictTypeService(model=DictType, args=None).get_detail(id)

        @ns.doc(parser=dicts_params['dict_type_params'], description="更新数据字典类型")
        @ns.expect(dicts_params['dict_type_params'])
        def put(self, id):
            """更新数据字典类型"""
            return DictTypeService(model=DictType, args=dicts_params['dict_type_params']).update(id)

        @ns.doc(description="删除数据字典类型")
        def delete(self, id):
            """删除数据字典类型"""
            return DictTypeService(model=DictType, args=None).delete(id)

    @ns.route("/types/tree")
    class DictTypeTreeResource(Resource):
        method_decorators = [casbin_required]

        def get(self):
            """获取数据字典类型树"""
            return DictTypeService(model=DictType, args=None).get_tree()

    @ns.route("/items")
    class DictItemListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=dicts_params['dict_list_params'], description="获取数据字典项列表")
        @ns.expect(dicts_params['dict_list_params'])
        def get(self):
            """获取数据字典项列表"""
            return DictService(model=Dict, args=dicts_params['dict_list_params'], only_parent=False).get_list()

        @ns.doc(parser=dicts_params['dict_params'], description="添加数据字典项")
        @ns.expect(dicts_params['dict_params'])
        def post(self):
            """添加数据字典项"""
            return DictService(model=Dict, args=dicts_params['dict_params']).create()

    @ns.route("/items/<int:id>")
    @ns.doc(params={"id": "数据字典项id"})
    class DictItemResource(Resource):
        method_decorators = [casbin_required]

        def get(self, id):
            """获取数据字典项详情"""
            return DictService(model=Dict, args=None).get_detail(id)

        @ns.doc(parser=dicts_params['dict_params'], description="更新数据字典项")
        @ns.expect(dicts_params['dict_params'])
        def put(self, id):
            """更新数据字典项"""
            return DictService(model=Dict, args=dicts_params['dict_params']).update(id)

        @ns.doc(description="删除数据字典项")
        def delete(self, id):
            """删除数据字典项"""
            return DictService(model=Dict, args=None).delete(id)

    @ns.route("/items/tree")
    class DictItemTreeResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=dicts_params['get_tree_params'], description="获取数据字典项树")
        @ns.expect(dicts_params['get_tree_params'])
        def get(self):
            """获取数据字典项树"""
            output_fields = ['id', 'name', 'value', 'label', 'parent_id', 'type_id', 'order']
            return DictService(model=Dict, args=dicts_params['get_tree_params']).get_tree(output_fields=output_fields)
